{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pd.Timestamp()-1: 2019-01-01 02:03:04\n",
      "pd.Timestamp()-2: 2019-01-01 02:03:04\n",
      "pd.Timestamp()-3: 2019-01-01 02:03:04\n",
      "pd.Timestamp()-type: <class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "pd.to_datetime()-1: 2019-01-01 00:01:01\n",
      "pd.to_datetime()-2: 2019-01-01 00:01:01\n",
      "pd.to_datetime()-type: <class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "pd.to_datetime()-list: DatetimeIndex(['2019-01-01 00:01:01', '2019-02-01 00:01:01',\n",
      "               '2019-03-01 00:01:01'],\n",
      "              dtype='datetime64[ns]', freq=None)\n",
      "month date_range():\n",
      "DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30',\n",
      "               '2019-05-31', '2019-06-30', '2019-07-31', '2019-08-31',\n",
      "               '2019-09-30', '2019-10-31', '2019-11-30', '2019-12-31'],\n",
      "              dtype='datetime64[ns]', freq='ME')\n",
      "month period_range():\n",
      "PeriodIndex(['2019-01', '2019-02', '2019-03', '2019-04', '2019-05', '2019-06',\n",
      "             '2019-07', '2019-08', '2019-09', '2019-10', '2019-11', '2019-12'],\n",
      "            dtype='period[M]')\n",
      "week date_range():\n",
      "DatetimeIndex(['2019-01-06', '2019-01-13', '2019-01-20', '2019-01-27',\n",
      "               '2019-02-03', '2019-02-10', '2019-02-17', '2019-02-24',\n",
      "               '2019-03-03', '2019-03-10', '2019-03-17', '2019-03-24'],\n",
      "              dtype='datetime64[ns]', freq='W-SUN')\n",
      "week period_range(): \n",
      "PeriodIndex(['2019-01', '2019-02', '2019-03', '2019-04', '2019-05', '2019-06',\n",
      "             '2019-07', '2019-08', '2019-09', '2019-10', '2019-11', '2019-12'],\n",
      "            dtype='period[M]')\n",
      "hour date_range():\n",
      "DatetimeIndex(['2019-01-01 00:00:00', '2019-01-01 01:00:00',\n",
      "               '2019-01-01 02:00:00', '2019-01-01 03:00:00',\n",
      "               '2019-01-01 04:00:00', '2019-01-01 05:00:00',\n",
      "               '2019-01-01 06:00:00', '2019-01-01 07:00:00',\n",
      "               '2019-01-01 08:00:00', '2019-01-01 09:00:00',\n",
      "               '2019-01-01 10:00:00', '2019-01-01 11:00:00'],\n",
      "              dtype='datetime64[ns]', freq='h')\n",
      "hour period_range(): \n",
      "PeriodIndex(['2019-01-01 00:00', '2019-01-01 01:00', '2019-01-01 02:00',\n",
      "             '2019-01-01 03:00', '2019-01-01 04:00', '2019-01-01 05:00',\n",
      "             '2019-01-01 06:00', '2019-01-01 07:00', '2019-01-01 08:00',\n",
      "             '2019-01-01 09:00', '2019-01-01 10:00', '2019-01-01 11:00'],\n",
      "            dtype='period[h]')\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Users\\yibozhang\\AppData\\Local\\Temp\\ipykernel_114212\\905541359.py:40: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  date_rng = pd.date_range('2019-01-01 00:00:00',freq = 'H',periods = 12)\n",
      "D:\\Users\\yibozhang\\AppData\\Local\\Temp\\ipykernel_114212\\905541359.py:43: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  period_rng = pd.period_range('2019-01-01 00:00:00', freq = 'H', periods = 12)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from datetime import date, time, datetime , timedelta\n",
    "# 年月日时分秒\n",
    "ts = pd.Timestamp(2019,1,1,2,3,4)\n",
    "print(f'pd.Timestamp()-1: {ts}')\n",
    "# datetime对象\n",
    "ts = pd.Timestamp(datetime(2019,1,1,hour=2,minute=3,second=4))\n",
    "print(f'pd.Timestamp()-2: {ts}')\n",
    "# 时间格式字符串\n",
    "ts = pd.Timestamp(\"2019-1-1 2:3:4\")\n",
    "print(f'pd.Timestamp()-3: {ts}')\n",
    "print(f'pd.Timestamp()-type: {type(ts)}')\n",
    "# datetime对象\n",
    "dt = pd.to_datetime(datetime(2019,1,1,hour=0,minute=1,second=1))\n",
    "print(f'pd.to_datetime()-1: {dt}')\n",
    "# 时间格式字符串\n",
    "dt = pd.to_datetime(\"2019-1-1 0:1:1\")\n",
    "print(f'pd.to_datetime()-2: {dt}')\n",
    "print(f'pd.to_datetime()-type: {type(dt)}')\n",
    "# 时间字符串转换为DatetimeIndex格式\n",
    "dtlist = pd.to_datetime([\"2019-1-1 0:1:1\", \"2019-2-1 0:1:1\", \"2019-3-1 0:1:1\"])\n",
    "print(f'pd.to_datetime()-list: {dtlist}')\n",
    "#时间偏移\n",
    "dt_0 = pd.to_datetime(datetime(2019, 1, 1, hour = 0, minute = 0, second = 0))\n",
    "dt_1 = dt_0 +pd.Timedelta(days=5, minutes=50, seconds=20)\n",
    "#时间范围序列\n",
    "#dtype=datetime64\n",
    "date_rng = pd.date_range('2019-01-01', freq = \"ME\", periods = 12)\n",
    "print(f'month date_range():\\n{date_rng}')\n",
    "#dtype=period\n",
    "period_rng = pd.period_range('2019-01-01', freq = 'M', periods = 12)\n",
    "print(f'month period_range():\\n{period_rng}')\n",
    "#w-sun周日的频率\n",
    "date_rng = pd.date_range('2019-01-01', freq='W-SUN', periods = 12)\n",
    "print(f'week date_range():\\n{date_rng}')\n",
    "\n",
    "peroid_rng = pd.period_range('2019-01-01', freq='W-SUN', periods=12)\n",
    "print(f'week period_range(): \\n{period_rng}')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hour date_range():\n",
      "DatetimeIndex(['2019-01-01 00:00:00', '2019-01-01 01:00:00',\n",
      "               '2019-01-01 02:00:00', '2019-01-01 03:00:00',\n",
      "               '2019-01-01 04:00:00', '2019-01-01 05:00:00',\n",
      "               '2019-01-01 06:00:00', '2019-01-01 07:00:00',\n",
      "               '2019-01-01 08:00:00', '2019-01-01 09:00:00',\n",
      "               '2019-01-01 10:00:00', '2019-01-01 11:00:00'],\n",
      "              dtype='datetime64[ns]', freq='h')\n",
      "hour period_range(): \n",
      "PeriodIndex(['2019-01-01 00:00', '2019-01-01 01:00', '2019-01-01 02:00',\n",
      "             '2019-01-01 03:00', '2019-01-01 04:00', '2019-01-01 05:00',\n",
      "             '2019-01-01 06:00', '2019-01-01 07:00', '2019-01-01 08:00',\n",
      "             '2019-01-01 09:00', '2019-01-01 10:00', '2019-01-01 11:00'],\n",
      "            dtype='period[h]')\n"
     ]
    }
   ],
   "source": [
    "\n",
    "date_rng = pd.date_range('2019-01-01 00:00:00',freq = 'h',periods = 12)\n",
    "print(f'hour date_range():\\n{date_rng}')\n",
    "\n",
    "period_rng = pd.period_range('2019-01-01 00:00:00', freq = 'h', periods = 12)\n",
    "print(f'hour period_range(): \\n{period_rng}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-01-01     1\n",
      "2019-01-02     2\n",
      "2019-01-03     3\n",
      "2019-01-04     4\n",
      "2019-01-05     5\n",
      "2019-01-06     6\n",
      "2019-01-07     7\n",
      "2019-01-08     8\n",
      "2019-01-09     9\n",
      "2019-01-10    10\n",
      "2019-01-11    11\n",
      "2019-01-12    12\n",
      "Freq: D, dtype: int64\n",
      "2019-01-01    15\n",
      "2019-01-06    40\n",
      "2019-01-11    23\n",
      "Freq: 5D, dtype: int64\n",
      "2019-01-01     1\n",
      "2019-01-06    20\n",
      "2019-01-11    45\n",
      "2019-01-16    12\n",
      "Freq: 5D, dtype: int64\n",
      "2019-01-01 00:00:00     1.0\n",
      "2019-01-01 12:00:00     NaN\n",
      "2019-01-02 00:00:00     2.0\n",
      "2019-01-02 12:00:00     NaN\n",
      "2019-01-03 00:00:00     3.0\n",
      "2019-01-03 12:00:00     NaN\n",
      "2019-01-04 00:00:00     4.0\n",
      "2019-01-04 12:00:00     NaN\n",
      "2019-01-05 00:00:00     5.0\n",
      "2019-01-05 12:00:00     NaN\n",
      "2019-01-06 00:00:00     6.0\n",
      "2019-01-06 12:00:00     NaN\n",
      "2019-01-07 00:00:00     7.0\n",
      "2019-01-07 12:00:00     NaN\n",
      "2019-01-08 00:00:00     8.0\n",
      "2019-01-08 12:00:00     NaN\n",
      "2019-01-09 00:00:00     9.0\n",
      "2019-01-09 12:00:00     NaN\n",
      "2019-01-10 00:00:00    10.0\n",
      "2019-01-10 12:00:00     NaN\n",
      "2019-01-11 00:00:00    11.0\n",
      "2019-01-11 12:00:00     NaN\n",
      "2019-01-12 00:00:00    12.0\n",
      "Freq: 12h, dtype: float64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Users\\yibozhang\\AppData\\Local\\Temp\\ipykernel_114212\\1748189006.py:11: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  ts_12h_asfreq = ts_d.resample('12H').asfreq()\n"
     ]
    }
   ],
   "source": [
    "#date_range生成一个时间区间段，间隔为天\n",
    "import numpy as np\n",
    "rng = pd.date_range('20190101', periods=12, freq = 'D')\n",
    "ts_d = pd.Series(np.arange(1, 13), index=rng)\n",
    "\n",
    "print(ts_d)\n",
    "#左闭右开 1 - 5，以五天的采样规则进行聚合\n",
    "print(ts_d.resample('5D',closed='left', label='left').sum()) \n",
    "#左开右闭2-6\n",
    "print(ts_d.resample('5D',closed='right',label = 'right').sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-01-01 00:00:00     1.0\n",
      "2019-01-01 12:00:00     NaN\n",
      "2019-01-02 00:00:00     2.0\n",
      "2019-01-02 12:00:00     NaN\n",
      "2019-01-03 00:00:00     3.0\n",
      "2019-01-03 12:00:00     NaN\n",
      "2019-01-04 00:00:00     4.0\n",
      "2019-01-04 12:00:00     NaN\n",
      "2019-01-05 00:00:00     5.0\n",
      "2019-01-05 12:00:00     NaN\n",
      "2019-01-06 00:00:00     6.0\n",
      "2019-01-06 12:00:00     NaN\n",
      "2019-01-07 00:00:00     7.0\n",
      "2019-01-07 12:00:00     NaN\n",
      "2019-01-08 00:00:00     8.0\n",
      "2019-01-08 12:00:00     NaN\n",
      "2019-01-09 00:00:00     9.0\n",
      "2019-01-09 12:00:00     NaN\n",
      "2019-01-10 00:00:00    10.0\n",
      "2019-01-10 12:00:00     NaN\n",
      "2019-01-11 00:00:00    11.0\n",
      "2019-01-11 12:00:00     NaN\n",
      "2019-01-12 00:00:00    12.0\n",
      "Freq: 12h, dtype: float64\n",
      "2019-01-01 00:00:00     1\n",
      "2019-01-01 12:00:00     1\n",
      "2019-01-02 00:00:00     2\n",
      "2019-01-02 12:00:00     2\n",
      "2019-01-03 00:00:00     3\n",
      "2019-01-03 12:00:00     3\n",
      "2019-01-04 00:00:00     4\n",
      "2019-01-04 12:00:00     4\n",
      "2019-01-05 00:00:00     5\n",
      "2019-01-05 12:00:00     5\n",
      "2019-01-06 00:00:00     6\n",
      "2019-01-06 12:00:00     6\n",
      "2019-01-07 00:00:00     7\n",
      "2019-01-07 12:00:00     7\n",
      "2019-01-08 00:00:00     8\n",
      "2019-01-08 12:00:00     8\n",
      "2019-01-09 00:00:00     9\n",
      "2019-01-09 12:00:00     9\n",
      "2019-01-10 00:00:00    10\n",
      "2019-01-10 12:00:00    10\n",
      "2019-01-11 00:00:00    11\n",
      "2019-01-11 12:00:00    11\n",
      "2019-01-12 00:00:00    12\n",
      "Freq: 12h, dtype: int64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Users\\yibozhang\\AppData\\Local\\Temp\\ipykernel_114212\\3331944420.py:8: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  ts_12h_ffill = ts_d.resample('12H').ffill()\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "rng = pd.date_range('20190101', periods=12, freq = 'D')\n",
    "ts_d = pd.Series(np.arange(1, 13), index=rng)\n",
    "#不填充\n",
    "ts_12h_asfreq = ts_d.resample('12h').asfreq()\n",
    "print(ts_12h_asfreq)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-01-01 00:00:00     1\n",
      "2019-01-01 12:00:00     1\n",
      "2019-01-02 00:00:00     2\n",
      "2019-01-02 12:00:00     2\n",
      "2019-01-03 00:00:00     3\n",
      "2019-01-03 12:00:00     3\n",
      "2019-01-04 00:00:00     4\n",
      "2019-01-04 12:00:00     4\n",
      "2019-01-05 00:00:00     5\n",
      "2019-01-05 12:00:00     5\n",
      "2019-01-06 00:00:00     6\n",
      "2019-01-06 12:00:00     6\n",
      "2019-01-07 00:00:00     7\n",
      "2019-01-07 12:00:00     7\n",
      "2019-01-08 00:00:00     8\n",
      "2019-01-08 12:00:00     8\n",
      "2019-01-09 00:00:00     9\n",
      "2019-01-09 12:00:00     9\n",
      "2019-01-10 00:00:00    10\n",
      "2019-01-10 12:00:00    10\n",
      "2019-01-11 00:00:00    11\n",
      "2019-01-11 12:00:00    11\n",
      "2019-01-12 00:00:00    12\n",
      "Freq: 12h, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "rng = pd.date_range('20190101', periods=12, freq = 'D')\n",
    "ts_d = pd.Series(np.arange(1, 13), index=rng)\n",
    "# 用前面的值填充无值的地方\n",
    "ts_12h_ffill = ts_d.resample('12h').ffill()\n",
    "print(ts_12h_ffill)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-01-01 00:00:00     1\n",
      "2019-01-01 12:00:00     2\n",
      "2019-01-02 00:00:00     2\n",
      "2019-01-02 12:00:00     3\n",
      "2019-01-03 00:00:00     3\n",
      "2019-01-03 12:00:00     4\n",
      "2019-01-04 00:00:00     4\n",
      "2019-01-04 12:00:00     5\n",
      "2019-01-05 00:00:00     5\n",
      "2019-01-05 12:00:00     6\n",
      "2019-01-06 00:00:00     6\n",
      "2019-01-06 12:00:00     7\n",
      "2019-01-07 00:00:00     7\n",
      "2019-01-07 12:00:00     8\n",
      "2019-01-08 00:00:00     8\n",
      "2019-01-08 12:00:00     9\n",
      "2019-01-09 00:00:00     9\n",
      "2019-01-09 12:00:00    10\n",
      "2019-01-10 00:00:00    10\n",
      "2019-01-10 12:00:00    11\n",
      "2019-01-11 00:00:00    11\n",
      "2019-01-11 12:00:00    12\n",
      "2019-01-12 00:00:00    12\n",
      "Freq: 12h, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "rng = pd.date_range('20190101', periods=12, freq = 'D')\n",
    "ts_d = pd.Series(np.arange(1, 13), index=rng)\n",
    "# 用后面的值填充无值的地方\n",
    "ts_12h_bfill = ts_d.resample('12h').bfill()\n",
    "print(ts_12h_bfill)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
